﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using Weather.com.Client;
using Weather.com.Client.WeatherConditions.Location;

namespace XsltWeatherResultTransformation
{
    public class Program
    {
        const string WEATHER_FORECAST_OUTPUT = "Paris_5Day_WeatherForecast.html";
        const string LOCATION_SEARCH_OUTPUT = "NewYorkLocations.html";
        const string CURRENT_CONDITIONS_OUTPUT = "Current_Weather_Conditions_In_Paris.html";

        private static void Main(string[] args)
        {
            WeatherClient weatherClient = new WeatherClient("Parner Id here", "License Key here");
            LocationSearchSample(weatherClient);
            CurrentConditionsSample(weatherClient);
            WeatherForecastSample(weatherClient);

            //Process.Start(LOCATION_SEARCH_OUTPUT);
            //Process.Start(CURRENT_CONDITIONS_OUTPUT);
            Process.Start(WEATHER_FORECAST_OUTPUT);
        }

        private static void CurrentConditionsSample(WeatherClient weatherClient)
        {
            string xsltPath = @"../../../XmlResponses/Xslt/CurrentConditions.xsl";
            XslCompiledTransform transformXmlToHtml = LoadXsltTransformation(xsltPath);
            List<Location> locations = weatherClient.GetLocation("Paris, France");
            string xml = weatherClient.GetCurrentConditionsAsXml(locations[0]);
            TransformXmlIntoHtml(transformXmlToHtml, xml, CURRENT_CONDITIONS_OUTPUT);
        }

        private static void WeatherForecastSample(WeatherClient weatherClient)
        {
            string xsltPath = @"../../../XmlResponses/Xslt/WeatherForecast.xsl";
            XslCompiledTransform transformXmlToHtml = LoadXsltTransformation(xsltPath);
            List<Location> locations = weatherClient.GetLocation("Paris, France");
            string xml = weatherClient.GetWeatherForecastAsXml(locations[0], 5);
            TransformXmlIntoHtml(transformXmlToHtml, xml, WEATHER_FORECAST_OUTPUT);
        }

        private static void LocationSearchSample(WeatherClient weatherClient)
        {
            string xsltPath = @"../../../XmlResponses/Xslt/LocationSearch.xsl";
            XslCompiledTransform transformXmlToHtml = LoadXsltTransformation(xsltPath);
            string xml = weatherClient.GetLocationAsXml("New York City");
            TransformXmlIntoHtml(transformXmlToHtml, xml, LOCATION_SEARCH_OUTPUT);
        }

        private static XslCompiledTransform LoadXsltTransformation(string xsltPath)
        {
            XmlUrlResolver resolver = new XmlUrlResolver();
            resolver.Credentials = CredentialCache.DefaultCredentials;
            XslCompiledTransform transformXmlToHtml = new XslCompiledTransform();
            transformXmlToHtml.Load(new XPathDocument(xsltPath), XsltSettings.Default, resolver);
            return transformXmlToHtml;
        }

        private static void TransformXmlIntoHtml(XslCompiledTransform transformXmlToHtml, string xml, string outputFileName)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            XmlReader reader = XmlReader.Create(new StringReader(xml));
            using (XmlWriter textWriter = XmlWriter.Create(outputFileName, settings))
            {
                transformXmlToHtml.Transform(reader, textWriter);
            }
        }
    }
}